package com.neuedu.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neuedu.common.ServerResponse;
import com.neuedu.mapper.DepartmentMapper;
import com.neuedu.pojo.Department;
import com.neuedu.service.IDepartmentService;

@Service
public class DepartmentServiceImpl implements IDepartmentService{
	
	//spring 依赖注入
	@Autowired
	private DepartmentMapper departmentMapper;
	
	@Override
	public ServerResponse<Object> add(Department department) {
		departmentMapper.insert(department);
		return ServerResponse.createBySuccess();
	}
	
	@Override
	public ServerResponse<Object> queryAll() {
		// TODO Auto-generated method stub
		return ServerResponse.createBySuccess(departmentMapper.selectAll());
	}

	@Override
	public ServerResponse<Object> update(Department department) {
		departmentMapper.update(department);
		return  ServerResponse.createBySuccess(department);
	}

	@Override
	public ServerResponse<Object> delete(Integer ID) {
		departmentMapper.delete(ID);
		return ServerResponse.createBySuccess();
	}
	//返回通用响应对象
	@Override
	public ServerResponse<Object> search(String keywords,Integer PageNum,Integer PageSize) {
		//使用pagehelper分页插件进行分页
		PageHelper.startPage(PageNum, PageSize);//查看第一页，每页显示3条
		List<Department> dps =  departmentMapper.selectByDeptCodeAndDeptName(keywords);
		//将查询结果封装到pageinfo中(总记录数、总页数、当前页、下一页、数据)
		PageInfo resultPage=new PageInfo<>(dps);
		return ServerResponse.createBySuccess(resultPage);
	}
	
	
}
